import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {_HttpClient, ModalHelper} from '@delon/theme';
import {STColumn, STComponent, STData} from '@delon/abc';
import {SFSchema} from '@delon/form';
import {NzModalRef} from "ng-zorro-antd";
import {DictService} from "@shared";
import {ActModelImageComponent} from "../image/image.component";

@Component({
  selector: 'app-act-model-version',
  templateUrl: './version.component.html',
})
export class ActModelVersionComponent implements OnInit {
  @Input()
  record: any = {};
  url = `act/Definition/listAllByKey`;
  searchSchema: SFSchema = {
    properties: {
      no: {
        type: 'string',
        title: '编号'
      }
    }
  };
  @ViewChild('st', {static: false}) st: STComponent;
  columns: STColumn[] = [
    {title: 'ID', index: 'id'},
    {title: '部署ID', index: 'deploymentId'},
    {title: '版本', index: 'version'},
    {
      title: '状态',
      index: 'suspensionState',
      format: (item: STData) => this.dictServcie.getDictValue("suspension_state", item.suspensionState)
    },

    {
      title: '操作',
      buttons: [
        {
          text: `删除`,
          type: 'none',
          click: (record) => this.http.post(`act/modeler/revokePublish?id=${record.deploymentId}`).subscribe(res => this.st.reload()),
        },
        {
          text: `激活`,
          type: 'none',
          click: (record) => this.http.post(`act/Definition/activateProcessDefinitionById?id=${record.id}`).subscribe(res => this.st.reload()),
          iif: (item: STData) => {
            return item.suspensionState !== 1;
          },
        },
        {
          text: `挂起`,
          type: 'none',
          click: (record) => this.http.post(`act/Definition/suspendProcessDefinitionById?id=${record.id}`).subscribe(res => this.st.reload()),
          iif: (item: STData) => {
            return item.suspensionState !== 2;
          },
        },
        {
          text: '查看流程图', type: 'modal',
          modal: {
            component: ActModelImageComponent,
            size:'xl'
          },
          click: 'reload'
        },
        // { text: '编辑', type: 'static', component: FormEditComponent, click: 'reload' },
      ]
    }
  ];

  constructor(private http: _HttpClient, private modal: ModalHelper, private ref: NzModalRef, private dictServcie: DictService,
  ) {
  }

  ngOnInit() {
    console.log(this.record);
    this.url = this.url + `?key=${this.record.key}`
  }

  add() {
    // this.modal
    //   .createStatic(FormEditComponent, { i: { id: 0 } })
    //   .subscribe(() => this.st.reload());
  }

  close() {
    this.ref.destroy();
  }
}
